package drds.data_migrate.util;


import drds.common.$;
import drds.common.Author;
import drds.data_migrate.Authors;
import drds.data_migrate.vo.TaskConfig;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Author(name = Authors.LI_YANG)
@Slf4j
public final class Writes
{


    public static void doPretreatment(TaskConfig taskConfig) throws SQLException
    {


        dealColumnConf(taskConfig);
        dealWriteMode(taskConfig);
    }


    public static void dealColumnConf(TaskConfig taskConfig) throws SQLException
    {

        Jdbc.JdbcCreateConnectionFactory jdbcCreateConnectionFactory = new Jdbc.JdbcCreateConnectionFactory(taskConfig.getJdbcUrl(), taskConfig.getUsername(), taskConfig.getPassword());
        dealColumnConf(jdbcCreateConnectionFactory, taskConfig.getTableName());
    }

    public static void dealColumnConf(Jdbc.JdbcCreateConnectionFactory createConnectionFactory, String tableName) throws SQLException
    {
        List<String> userConfiguredColumnNameList = null;
        if (!$.isHasData(userConfiguredColumnNameList))
        {
            throw new IllegalArgumentException("列配置信息有误");
        }
        List<String> columnNameList = Jdbcs.getColumnNameList(createConnectionFactory.getConnection(), tableName);

        if (1 == userConfiguredColumnNameList.size() && "*".equals(userConfiguredColumnNameList.get(0)))
        {
            throw new IllegalArgumentException("*");
        } else if (userConfiguredColumnNameList.size() > columnNameList.size())
        {
            throw new IllegalArgumentException(String.format("您的配置文件中的列配置信息有误. 因为您所配置的写入数据库表的字段个数:%s 大于目的表的总字段总个数:%s. 请检查您的配置并作出修改.",
                    userConfiguredColumnNameList.size(), columnNameList.size()));
        } else
        {
            // 确保用户配置的 table_column_name_list 不重复
            makeSureNoValueDuplicate(userConfiguredColumnNameList);

            // 检查列是否都为数据库表中正确的列（通过执行一次 select table_column_name_list from tableName 进行判断）
        }
    }


    public static void dealWriteMode(TaskConfig taskConfig)
    {
        List<String> columnNameList = null;


        List<String> valueList = new ArrayList<String>(columnNameList.size());
        for (int i = 0; i < columnNameList.size(); i++)
        {
            valueList.add("?");
        }
// 默认为：insert 方式
        String writeMode = taskConfig.getWriteMode();// taskConfig.getString(Key.write_mode, "INSERT");
        boolean forceUseUpdate = false;
        String sql = Updates.getWriteTemplate(columnNameList, valueList, writeMode, forceUseUpdate);
        taskConfig.setInsert_or_replace_template(sql);
    }


    public static void makeSureNoValueDuplicate(List<String> stringList)
    {
        if (null == stringList || stringList.isEmpty())
        {
            throw new IllegalArgumentException("您提供的作业配置有误, List不能为空.");
        }

        if (1 == stringList.size())
        {
            return;
        } else
        {
            Set<String> set = new HashSet<String>(stringList);
            if (set.size() != stringList.size())
            {
                throw new IllegalArgumentException("字段重复");
            }
        }
    }
}
